module (...,package.seeall)
	local storyboard = require( "storyboard" )
	local scene = storyboard.newScene()
	local ui = require ("ui")
	local wEasing = require "wEasing"
	local gui = require "Stage4.Stage4HomeGui".new()
	system.activate("multitouch")
function scene:createScene(event)
	local group = self.view
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
	--local group = self.view

	-- audio.play( sfx.themeMusic, { onComplete = function() 
                                      -- audio.dispose( sfx.themeMusic ) 
                                      -- end } )
	-----------------------------------------------------------------------------
	local mainHomeGroup = self.view
	local arrayCloset = {}	--mang vi tri dat quan ao o tren gia
	local arrayClothes = {} -- mang cac loai quan ao
	local arrayButton = {}	--mang nut
	local arrayClothesBtn= {} -- mang cac loai nut
	local arrayCountItem = {0,0,0}
	local arrayViewCloset = {} --mang quan ao o tren giá
	local countClothes = 3---so luon item moi luot choi
	local arrayBtn = {}--mang vi tri cac nut
	local random1 = 0
	local random2 = 0
	local delay = 100
	local loop = 0 
	local timeReady = 1
	local deltaTime = 5100
	local countturn = 0
	local timeScore = 0
	local timeTotalScore = 0
	local statusBoard = "start"
	local arrayTimeScore = {}
	local xCenter = display.contentCenterX
	local yCenter = display.contentCenterY 
	local timer_countdown
	local timer_countdownReady
	local bg
	--local txtCount = {}
	local arrayCount = {0,0,0}
	local txtTime, txtTimeReady, txtTimeScore, gameOver, win
	local btnPause
	local keyListener = false
	local time1
	local t,numberStar
	local arrayScore = { 3.0, 2.5, 2.0, 1.5, 1.0}
	local iconOk, iconGood, iconPerfect
	-----------------------HÀM SINH NGẪU NHIÊN CẢNH CHƠI-----------------------------
	function ViewCloset()
		txtTimeScore.alpha = 1
		statusBoard = "play"
		countClothes = countClothes
		randomClother()
			arrayViewCloset[1] = display.newImageRect(mainHomeGroup, arrayClothes[random1].fn, arrayClothes[random1].w, arrayClothes[random1].h)
			arrayViewCloset[1].x = arrayCloset[1].x
			arrayViewCloset[1].y = arrayCloset[1].y
			arrayCloset[1].id = random1
			arrayButton[1] = display.newImageRect(mainHomeGroup,arrayClothesBtn[random1].fn, arrayClothesBtn[random1].w, arrayClothesBtn[random1].h)
			arrayButton[1].x = arrayBtn[1].x
			arrayButton[1].y = arrayBtn[1].y
			arrayButton[1].id = random1
			
			arrayViewCloset[2] = display.newImageRect(mainHomeGroup, arrayClothes[random2].fn, arrayClothes[random2].w, arrayClothes[random2].h)
			arrayViewCloset[2].x = arrayCloset[2].x
			arrayViewCloset[2].y = arrayCloset[2].y
			arrayCloset[2].id = random2
			arrayButton[2] = display.newImageRect(mainHomeGroup,arrayClothesBtn[random2].fn, arrayClothesBtn[random2].w, arrayClothesBtn[random2].h)
			arrayButton[2].x = arrayBtn[2].x
			arrayButton[2].y = arrayBtn[2].y
			arrayButton[2].id = random2			
		vt = 3
		for i = 3,countClothes do
			random3 = math.random(1,6)
			arrayViewCloset[i] = display.newImageRect(mainHomeGroup, arrayClothes[random3].fn,arrayClothes[random3].w, arrayClothes[random3].h)
			arrayViewCloset[i].x = arrayCloset[vt].x
			arrayViewCloset[i].y = arrayCloset[vt].y
			arrayCloset[i].id = random3
			vt = vt + 1
		end
		random4 = math.random(1,6)
		while random4 == random1 or random4 == random2 do
			random4 = math.random(1,6)
		end
		if random4 ~= random1 and random4~= random2 then
				arrayButton[3] = display.newImageRect(mainHomeGroup,arrayClothesBtn[random4].fn, arrayClothesBtn[random4].w, arrayClothesBtn[random4].h)
				arrayButton[3].x = arrayBtn[3].x
				arrayButton[3].y = arrayBtn[3].y
				arrayButton[3].id = random4
		end
		for i = 1,12 do
			if arrayCloset[i].id == arrayButton[1].id then
				arrayCountItem[1] = arrayCountItem[1]+1 
			end
			if arrayCloset[i].id == arrayButton[2].id then
				arrayCountItem[2] = arrayCountItem[2]+1
			end
			if arrayCloset[i].id == arrayButton[3].id then
				arrayCountItem[3] = arrayCountItem[3]+1
			end			
		end
		-- txtCount[1].text = arrayCountItem[1]
		-- txtCount[2].text = arrayCountItem[2]
		-- txtCount[3].text = arrayCountItem[3]
		if keyListener == false then
			for i = 1,3 do
				arrayButton[i]:addEventListener("touch", onTouchButton)
			end
			keyListener = true
		end
	end
	function onReplayButtonTap()
		if keyListener == true then
			for k,v in pairs(arrayButton) do
				v:removeEventListener("touch", onTouchButton)
			end
			keyListener = false
		end
		Init()
	end
	onStartPopupPause = function()
		if(timer_countdown ~= nil)then
			timer.pause(timer_countdown)
		end
		if (timer_countdownReady ~= nil ) then
			timer.pause(timer_countdownReady)
		end
		if keyListener == true then
			for i = 1,3 do
				if arrayButton[i] ~= nil then
					arrayButton[i]:removeEventListener("touch", onTouchButton)
				end
			end
			keyListener = false
		end
	end
	onResumePopup = function()
		if(timer_countdown ~= nil)then
			timer.resume(timer_countdown)
		end
		if(timer_countdownReady ~= nil)then
			timer.resume(timer_countdownReady)
		end
		if keyListener == false and statusBoard == "play" then
			for i = 1,3 do
				if arrayButton[i] ~= nil then
					arrayButton[i]:addEventListener("touch", onTouchButton)
				end
			end
			keyListener = true
		end
	end
	function Pause()
		local g1 = require "popup.popupPause".new(onResumePopup,onReplayButtonTap)
			local group1 = display.newGroup()
			group1:insert(g1)
			mainHomeGroup:insert(group1)
			group1.y = -300
			transition.to(group1,{time = 200,alpha = 1, x = 0, y = 0, transition = wEasing.easeOutElastic, onStart = onStartPopupPause})
		
	end
	-----SỰ KIỆN TAP-----------------------
	function onTouchButton(event)
		phase = event.phase
		if phase == "began" then
			t = event.target
			--print (t.id)
			if t.id == arrayButton[1].id  then
				arrayCountItem[1] = arrayCountItem[1] - 1
				--txtCount[1].text = arrayCountItem[1]
			elseif t.id == arrayButton[2].id then
				arrayCountItem[2] = arrayCountItem[2] - 1
				--txtCount[2].text = arrayCountItem[2]
			elseif t.id == arrayButton[3].id then
				arrayCountItem[3] = arrayCountItem[3] - 1
				--txtCount[3].text = arrayCountItem[3]
			end
			checkWin()
			for i =1,3 do
				if arrayCountItem[i] < 0 then
					gameOver.alpha = 1
					mainHomeGroup:insert(gameOver)
					timer.cancel(timer_countdown)
					if keyListener == true then
						for j = 1,3 do
							arrayButton[j]:removeEventListener("touch", onTouchButton)
						end
						keyListener = false
					end
				end
			end
		end
	end
	function finishedGame()
		if timeScore > 3.5 then
			numberStar = 0
		elseif timeScore > 2.5 and timeScore<=3.5 then
			numberStar = 1
		elseif timeScore >2.0 and timeScore <= 2.5 then
			numberStar = 2
		elseif timeScore > 1.5 and timeScore <=2.0  then
			numberStar = 3
		elseif timeScore > 1.0 and timeScore <= 1.5 then
			numberStar = 4
		elseif timeScore <= 1.0 then
			numberStar = 5
		end
		local g1
			g1 = require "popup.popupWin".new(timeScore,numberStar,arrayScore,onReplayButtonTap)
		transition.to(g1,{time = 200,alpha = 1, x = 0, y = 0, transition = wEasing.easeOutElastic})
	end
	--------------KIỂM TRA THẮNG----------------
	function checkWin()
		for i = 1,10 do
			arrayTimeScore[i] = 0
		end
		if arrayCountItem[1] == 0 and arrayCountItem[2] == 0 and arrayCountItem[3] == 0 then
			statusBoard = "wait"
			countturn = countturn+1
			arrayTimeScore[countturn] = (math.round((deltaTime-time1)/100))/10
			timer.cancel(timer_countdown)
			print("countturn = "..countturn)
			print (arrayTimeScore[countturn])
			for i = 1,10 do
				timeTotalScore = timeTotalScore + arrayTimeScore[i]
				i = i+1
			end
			if arrayTimeScore[countturn] <= 2 then
				iconPerfect.alpha = 1
				iconGood.alpha = 0
				iconOk.alpha = 0
			elseif arrayTimeScore[countturn]>2 and arrayTimeScore[countturn] <=3.5 then
				iconGood.alpha = 1
				iconOk.alpha = 0
				iconPerfect.alpha = 0
			else
				iconOk.alpha = 1
				iconGood.alpha = 0
				iconPerfect.alpha = 0
			end
			print("timeTotalScore = "..timeTotalScore)
			timeScore = math.round((timeTotalScore/countturn)*10)/10
			txtTimeScore.text = timeScore
			txtTime.alpha = 0
			for i = 1,3 do
				display.remove(arrayButton[i])
				keyListener = false
			end
			for i = 1,12 do
				display.remove(arrayViewCloset[i])
				arrayCloset[i].id = nil
			end
			if countturn < 10 then
				--win.alpha = 1
				timeReady = 1
				if countturn<6 then
					deltaTime = deltaTime -200
				end
				txtTimeReady.alpha = 0
				timer_countdownReady = timer.performWithDelay(1000,countdownReady,0)
				if countClothes <7 then
					countClothes = countClothes+2
				elseif countClothes<12 then
					countClothes = countClothes+1
				end
			else
				timefinish = timer.performWithDelay(300,function()
																	finishedGame()
																end ,1)
			end
		end
	end
	-------------THOI GIAN CHOI-----------------
	
	function countdownTime (event)
		txtTime.alpha = 1
		time1 = time1-100
		 txtTime.text = (math.round((time1)/100))/10 ..""
		 if( time1 <0) then
			  txtTime.text = "0"
			  timer.cancel(timer_countdown)
			  gameOver.alpha = 1
			  mainHomeGroup:insert(gameOver)
				if keyListener == true then
					for i = 1,3 do
						arrayButton[i]:removeEventListener("touch", onTouchButton)
					end
					keyListener = false
				end
		end
	end
	
	----THOI GIAN BAT DAU VAO CHOI-------------------
	function countdownReady ()
		timeReady = timeReady - 1
			txtTimeReady.text = timeReady
			if txtTimeReady ~= nil and statusBoard== "start" then
				--txtTimeReady.alpha = 1
			end
		if timeReady <1 then
			if txtTimeReady ~= nil then
				txtTimeReady.alpha = 0
			end
			win.alpha = 0
			iconOk.alpha = 0
			iconGood.alpha = 0
			iconPerfect.alpha = 0
			go.alpha = 1
		end
		if timeReady <0 then
			win.alpha = 0
			go.alpha = 0
			timer.cancel(timer_countdownReady)
			ViewCloset()
			time1 = deltaTime
			timer_countdown = timer.performWithDelay(100, countdownTime, loop)
			t = system.getTimer()
		end
	end
	timer_countdownReady = timer.performWithDelay(1000,countdownReady,0)
	function randomClother()
		random1 = math.random(1,6)
		random2 = math.random(1,6)
		while ( random2 == random1 ) do
			random2 = math.random(1,6)
		end
	end
	function Init()
		iconOk.alpha = 0
		iconGood.alpha = 0
		iconPerfect.alpha = 0
		countClothes = 3
		timeReady = 1
		txtTimeReady.text = "3"
		statusBoard = "again"
		txtTimeReady.alpha = 1
		txtTime.alpha = 0
		deltaTime = 5100
		countturn = 0
		gameOver.alpha = 0
		win.alpha = 0
		timeScore = 0
		timeTotalScore = 0
		txtTimeScore.alpha = 0
		txtTimeScore.text = timeScore
		for i = 1,3 do
			arrayCountItem[i] = 0
			txtCount[i].text = arrayCountItem[i]
		end
		if timer_countdown ~= nil then
			timer.cancel(timer_countdown)
		end
		if timer_countdownReady ~= nil then
			timer.cancel(timer_countdownReady)
		end
		for i = 1,3 do
			display.remove(arrayButton[j])
			keyListener = false
		end
		for i = 1,12 do
			display.remove(arrayViewCloset[i])
			arrayCloset[i].id = nil
		end
		timer_countdownReady = timer.performWithDelay(1000,countdownReady,0)
	end
	a = 0
	for i = 1,4 do
		arrayCloset[i] = display.newImageRect(mainHomeGroup,gui.map.fn,gui.map.w, gui.map.h)
			arrayCloset[i].x = gui.map.x+ a
			arrayCloset[i].y = gui.map.y
			a = a +65
			arrayCloset[i].alpha = 0.2
	end
	a = 0
	for i = 5,8 do
		arrayCloset[i] = display.newImageRect(mainHomeGroup,gui.map.fn,gui.map.w, gui.map.h)
			arrayCloset[i].x = gui.map.x+ a
			arrayCloset[i].y = gui.map.y +92
			a = a +65
			arrayCloset[i].alpha = 0.2
	end
	 a = 0
	for i = 9,12 do
		arrayCloset[i] = display.newImageRect(mainHomeGroup,gui.map.fn,gui.map.w, gui.map.h)
			arrayCloset[i].x = gui.map.x+ a
			arrayCloset[i].y = gui.map.y +184
			a = a +65
			arrayCloset[i].alpha = 0.2
	end
	
	----------------thiet lap list quan ao--------------------
	function list()
		arrayClothes[1] = display.newImageRect(mainHomeGroup,gui.coats1.fn, gui.coats1.w, gui.coats1.h)
		arrayClothes[1].fn = gui.coats1.fn
		arrayClothes[1].w = gui.coats1.w
		arrayClothes[1].h = gui.coats1.h
		arrayClothes[1].id = 1
		arrayClothes[2] = display.newImageRect(mainHomeGroup, gui.coats2.fn, gui.coats2.w, gui.coats2.h)
		arrayClothes[2].fn = gui.coats2.fn
		arrayClothes[2].w = gui.coats2.w
		arrayClothes[2].h = gui.coats2.h
		arrayClothes[2].id = 2
		arrayClothes[3]= display.newImageRect(mainHomeGroup,gui.dress1.fn, gui.dress1.w, gui.dress1.h)
		arrayClothes[3].fn = gui.dress1.fn
		arrayClothes[3].w = gui.dress1.w
		arrayClothes[3].h = gui.dress1.h
		arrayClothes[3].id = 3
		arrayClothes[4] = display.newImageRect(mainHomeGroup,gui.dress2.fn, gui.dress2.w, gui.dress2.h)
		arrayClothes[4].fn = gui.dress2.fn
		arrayClothes[4].w = gui.dress2.w
		arrayClothes[4].h = gui.dress2.h
		arrayClothes[4].id = 4
		arrayClothes[5] = display.newImageRect(mainHomeGroup,gui.pants1.fn, gui.pants1.w, gui.pants1.h)
		arrayClothes[5].fn = gui.pants1.fn
		arrayClothes[5].w = gui.pants1.w
		arrayClothes[5].h = gui.pants1.h
		arrayClothes[5].id = 5
		arrayClothes[6] = display.newImageRect(mainHomeGroup,gui.pants2.fn, gui.pants2.w, gui.pants2.h)
		arrayClothes[6].fn = gui.pants2.fn
		arrayClothes[6].w = gui.pants2.w
		arrayClothes[6].h = gui.pants2.h
		arrayClothes[6].id = 6
		----------------------list quan ao tren nut--------------------------
		arrayClothesBtn[1] = display.newImageRect(mainHomeGroup,gui.btnCoats1.fn, gui.btnCoats1.w, gui.btnCoats1.h)
		arrayClothesBtn[1].fn = gui.btnCoats1.fn
		arrayClothesBtn[1].w = gui.btnCoats1.w
		arrayClothesBtn[1].h = gui.btnCoats1.h
		arrayClothesBtn[1].id = 1
		arrayClothesBtn[2] = display.newImageRect(mainHomeGroup, gui.btnCoats2.fn, gui.btnCoats2.w, gui.btnCoats2.h)
		arrayClothesBtn[2].fn = gui.btnCoats2.fn
		arrayClothesBtn[2].w = gui.btnCoats2.w
		arrayClothesBtn[2].h = gui.btnCoats2.h
		arrayClothesBtn[2].id = 2
		arrayClothesBtn[3]= display.newImageRect(mainHomeGroup,gui.btnDress1.fn, gui.btnDress1.w, gui.btnDress1.h)
		arrayClothesBtn[3].fn = gui.btnDress1.fn
		arrayClothesBtn[3].w = gui.btnDress1.w
		arrayClothesBtn[3].h = gui.btnDress1.h
		arrayClothesBtn[3].id = 3
		arrayClothesBtn[4] = display.newImageRect(mainHomeGroup,gui.btnDress2.fn, gui.btnDress2.w, gui.btnDress2.h)
		arrayClothesBtn[4].fn = gui.btnDress2.fn
		arrayClothesBtn[4].w = gui.btnDress2.w
		arrayClothesBtn[4].h = gui.btnDress2.h
		arrayClothesBtn[4].id = 4
		arrayClothesBtn[5] = display.newImageRect(mainHomeGroup,gui.btnPants1.fn, gui.btnPants1.w, gui.btnPants1.h)
		arrayClothesBtn[5].fn = gui.btnPants1.fn
		arrayClothesBtn[5].w = gui.btnPants1.w
		arrayClothesBtn[5].h = gui.btnPants1.h
		arrayClothesBtn[5].id = 5
		arrayClothesBtn[6] = display.newImageRect(mainHomeGroup,gui.btnPants2.fn, gui.btnPants2.w, gui.btnPants2.h)
		arrayClothesBtn[6].fn = gui.btnPants2.fn
		arrayClothesBtn[6].w = gui.btnPants2.w
		arrayClothesBtn[6].h = gui.btnPants2.h
		arrayClothesBtn[6].id = 6
		for i = 1,6 do
			arrayClothesBtn[i].alpha = 0
		end
		----------------------------------------
		for i = 1,6 do
			arrayClothes[i].isVisible = false
		end
		a = 0
		for i = 1,3 do 
			arrayBtn[i] = display.newImageRect(mainHomeGroup,gui.btnCoats1.fn, gui.btnCoats1.w, gui.btnCoats1.h)
			arrayBtn[i].x = gui.btnCoats1.x+a
			arrayBtn[i].y = gui.btnCoats1.y
			a = a+100
			arrayBtn[i].alpha = 0
		end
	end
	
	bg = display.newImageRect(mainHomeGroup,gui.bg.fn, gui.bg.w, gui.bg.h)
		bg.x = gui.bg.x
		bg.y = gui.bg.y
	list()
	txtTime = display.newText(mainHomeGroup, deltaTime/1000, gui.txtTime.x+50, gui.txtTime.y, native.systemFont, gui.txtTime.size-10)
		txtTime:setTextColor(0,0,0)
		txtTime.alpha = 0
	txtTimeScore = display.newText(mainHomeGroup,timeScore, gui.txtTimeScore.x, gui.txtTimeScore.y, gui.txtTimeScore.font, gui.txtTimeScore.size)
		txtTimeScore:setTextColor(unpack(gui.txtTimeScore.color))
		txtTimeScore.alpha = 0
	iconOk = display.newImageRect(mainHomeGroup, gui.iconOk.fn, gui.iconOk.w, gui.iconOk.h)
		iconOk.x = gui.iconOk.x
		iconOk.y = gui.iconOk.y
		iconOk.alpha= 0
	iconGood = display.newImageRect(mainHomeGroup, gui.iconGood.fn, gui.iconGood.w, gui.iconGood.h)
		iconGood.x = gui.iconGood.x
		iconGood.y = gui.iconGood.y
		iconGood.alpha= 0
	iconPerfect = display.newImageRect(mainHomeGroup, gui.iconPerfect.fn, gui.iconPerfect.w, gui.iconPerfect.h)
		iconPerfect.x = gui.iconPerfect.x
		iconPerfect.y = gui.iconPerfect.y
		iconPerfect.alpha= 0
	--local txtCount = {}
	-- txtCount[1] = display.newText(mainHomeGroup,arrayCount[1], 90, 380, native.systemFont,20)
		-- txtCount[1]:setTextColor(0,0,0)
	-- txtCount[2] = display.newText(mainHomeGroup,arrayCount[2],190,380,native.systemFont,20)
		-- txtCount[2]:setTextColor(0,0,0)
	-- txtCount[3] = display.newText(mainHomeGroup,arrayCount[3],290,380,native.systemFont,20)
		-- txtCount[3]:setTextColor(0,0,0)
	txtTimeReady = display.newText(mainHomeGroup, timeReady,xCenter-15 , 150, gui.txtTime.font, gui.txtTime.size+20)
		txtTimeReady:setTextColor(unpack(gui.txtTime.color))
		txtTimeReady.alpha = 0
	go = display.newImageRect(mainHomeGroup, gui.go.fn, gui.go.w, gui.go.h)
		go.x = gui.go.x
		go.y = gui.go.y
		go.alpha = 0
	gameOver = display.newText(mainHomeGroup,"GAME OVER", gui.txtTime.x, 120, gui.txtTime.font, gui.txtTime.size+20)
		gameOver.x = display.contentCenterX
		gameOver.y = display.contentCenterY
		gameOver:setTextColor(unpack(gui.txtTime.color))
		gameOver.alpha = 0
	win = display.newText(mainHomeGroup,"you win", gui.txtTime.x, 120, gui.txtTime.font, gui.txtTime.size+20)
		win.x = display.contentCenterX
		win.y = display.contentCenterY
		win:setTextColor(unpack(gui.txtTime.color))
		win.alpha = 0
	btnPause = ui.newButton({ defaultSrc = gui.btnPause.fn,
											overSrc = gui.btnPause.fn,
											defaultX = gui.btnPause.w,
											defaultY = gui.btnPause.h,
											overX = gui.btnPause.w,
											overY = gui.btnPause.h,
											overAlpha = gui.btnPause.overAlpha,
											overScale = gui.btnPause.overScale,
											x = gui.btnPause.x,
											y = gui.btnPause.y,
											onRelease = Pause
										})
		mainHomeGroup:insert(btnPause)
	return mainHomeGroup

	
	-----------------------------------------------------------------------------
	
end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )
	local group = self.view
	
	-----------------------------------------------------------------------------
	
	--	INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
	
	-----------------------------------------------------------------------------
	
end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
	local group = self.view
	
	-----------------------------------------------------------------------------
	
	--	INSERT code here (e.g. remove listeners, widgets, save state, etc.)
	
	-----------------------------------------------------------------------------
	
end

---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

---------------------------------------------------------------------------------

return scene
